package com.zzq.subservice.impl;

import com.zzq.annotation.SubService;
import com.zzq.domain.bo.SmileFeeBO;
import com.zzq.domain.dto.SmileCouponDTO;
import com.zzq.domain.dto.SmileShootAddressDTO;
import com.zzq.domain.dto.SmileShootRecordDTO;
import com.zzq.domain.entity.SmileFee;
import com.zzq.domain.search.ShootAddressSearchParamet;
import com.zzq.domain.search.ShootRecordSearchParament;
import com.zzq.enumerate.CouponTypeEnum;
import com.zzq.enumerate.ShootTypeEnum;
import com.zzq.service.SmileCouponService;
import com.zzq.service.SmileShootAddressService;
import com.zzq.service.SmileShootRecordService;
import com.zzq.subservice.CostcalculcationSubService;
import com.zzq.util.BeanCopyUtils;
import com.zzq.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
@SubService
public class CostcalculcationSubServiceImpl implements CostcalculcationSubService {
    @Autowired
    SmileShootRecordService recordService;
    @Autowired
    SmileShootAddressService shootAddressService;
    @Autowired
    SmileCouponService couponService;


    public SmileFeeBO getAmountSmileFeeDTO(ShootRecordSearchParament recordSearchParament) {

        //获取之前的订单
        ShootRecordSearchParament shootRecordSearchParament = new ShootRecordSearchParament();
        shootRecordSearchParament.setPay(true);
        //已解锁数量
        Integer choosedNum = 0;
        List<SmileShootRecordDTO> smileShootRecordDTOs = recordService.getSmileShootRecordDTOs(shootRecordSearchParament);
        if (!ObjectUtils.isEmpty(smileShootRecordDTOs)) {
            for (SmileShootRecordDTO smileShootRecordDTO : smileShootRecordDTOs) {
                choosedNum = choosedNum + smileShootRecordDTO.getChooseNum();
            }
        }
        //生成新的订单
        Long shootAddressId = recordSearchParament.getShootAddressId();
        List<Long> photoIds = recordSearchParament.getPhotoIds();
        int choesphotosNum = photoIds.size();
        ShootAddressSearchParamet searchParamet = new ShootAddressSearchParamet();
        searchParamet.setShootAddressId(shootAddressId);
        List<SmileShootAddressDTO> shootAddressDTOS = shootAddressService.getShootAddressDTO(searchParamet);
        SmileShootAddressDTO smileShootAddressDTO = shootAddressDTOS.get(0);
        SmileFee smileFee = smileShootAddressDTO.getSmileFee();

        String feeType = smileFee.getFeeType();
        SmileFeeBO smileFeeBo = BeanCopyUtils.copyBean(smileFee, SmileFeeBO.class);
        smileFeeBo.setSize(choosedNum);
        if (feeType.equals(ShootTypeEnum.FEESECOND.getName())) {
            BigDecimal feeAmount = smileFeeBo.getFeeAmount();
            smileFeeBo.setAmount(feeAmount);

        } else {
            smileFeeBo = getfeeAnnexationAmount(smileFeeBo, choesphotosNum);
        }

        //优惠券
        if (!ObjectUtils.isEmptys(recordSearchParament.getFavorable(), recordSearchParament.getCouponNo())
                && recordSearchParament.getFavorable()) {
            String couponNo = recordSearchParament.getCouponNo();
            SmileCouponDTO couponDTO = couponService.getCouponDTObyNo(couponNo);
            if (!ObjectUtils.isEmpty(couponDTO)) {
                BigDecimal amount = smileFeeBo.getAmount();
                BigDecimal amountFull = couponDTO.getAmountFull();
                //满减
                if (amountFull.compareTo(amount) < 1) {
                    if (CouponTypeEnum.FAVORABLE.getName().equals(couponDTO.getCouponType())) {
                        BigDecimal couponFavorable = couponDTO.getCouponFavorable();
                        BigDecimal favorableAmount = amount.subtract(couponFavorable);
                        smileFeeBo.setFavorableAmount(couponFavorable);
                        smileFeeBo.setAmount(favorableAmount);

                    } else if (CouponTypeEnum.DISCOUNT.getName().equals(couponDTO.getCouponType())) {
                        BigDecimal couponDiscount = couponDTO.getCouponDiscount();
                        BigDecimal discountAmount = amount.multiply(couponDiscount);
                        BigDecimal favorableAmount = amount.subtract(discountAmount);
                        smileFeeBo.setFavorableAmount(favorableAmount);
                        smileFeeBo.setAmount(discountAmount);
                    }
                }


            }
        }

        return smileFeeBo;
    }

    //计算存在附加
    public  SmileFeeBO getfeeAnnexationAmount(SmileFeeBO smileFeeBo, int choesphotosNum) {
        if (smileFeeBo.getFeeAnnexation()) {
            //拍满数量
            Integer feeShootFull = smileFeeBo.getFeeShootFull();
            //附赠数量
            Integer feeAnnexationNum = smileFeeBo.getFeeAnnexationNum();
            //套餐3 已经解锁的数量>满拍数量 只考虑多余
            if (smileFeeBo.getSize() >= feeShootFull) {

                //已经解锁的数量-
                int size = smileFeeBo.getSize() - feeAnnexationNum;
                if (size >= feeShootFull) {
                    return getfeeAmount(smileFeeBo, choesphotosNum);
                } else {
                    //剩余赠送数量
                    int lateAnnexationNum = feeShootFull - size;
                    //应结算数量
                    choesphotosNum = choesphotosNum - lateAnnexationNum;
                    return getfeeAmount(smileFeeBo, choesphotosNum);
                }
            } else if (smileFeeBo.getSize() > 0) {
                if (smileFeeBo.getSize() + choesphotosNum > feeShootFull) {
                    if (smileFeeBo.getSize() + choesphotosNum - feeShootFull < feeAnnexationNum) {
                        //选择数量超过拍满数量但是没有超过附赠+拍满数量，价格=拍满数量
                        int i = smileFeeBo.getSize() + choesphotosNum - feeShootFull;
                        return getfeeAmount(smileFeeBo, feeShootFull - smileFeeBo.getSize() + feeAnnexationNum - i);
                    } else {
                        //选择数量超过拍满数量且超过 附赠+拍满数量，价格=拍满数量
                        return getfeeAmount(smileFeeBo, feeShootFull - smileFeeBo.getSize() + choesphotosNum - feeAnnexationNum);
                    }
                }
            }
        }
        return getfeeAmount(smileFeeBo, choesphotosNum);
    }

    //计算不存在附加
    public  SmileFeeBO getfeeAmount(SmileFeeBO smileFeeBo, int choesphotosNum) {
        BigDecimal feeAmount = smileFeeBo.getFeeAmount();
        //最高拍摄数量
        Integer feeShootMax = smileFeeBo.getFeeShootMax();
        //额外的拍摄费用
        BigDecimal feeAmountFull = smileFeeBo.getFeeAmountFull();

        //数量>=最高拍摄数量
        if (smileFeeBo.getSize() >= feeShootMax) {

            feeAmountFull = feeAmountFull.multiply(BigDecimal.valueOf(choesphotosNum));
            smileFeeBo.setAmount(feeAmountFull);
            smileFeeBo.setSize(smileFeeBo.getSize() + choesphotosNum);
            return smileFeeBo;
        } else if (smileFeeBo.getSize() > 0) {
            //全部拍摄数量
            if (smileFeeBo.getSize() + choesphotosNum > feeShootMax) {
                int outNum = (smileFeeBo.getSize() + choesphotosNum) - feeShootMax;
                feeAmountFull = feeAmountFull.multiply(BigDecimal.valueOf(outNum));
                smileFeeBo.setAmount(feeAmountFull);
                smileFeeBo.setSize(smileFeeBo.getSize() + choesphotosNum);
                return smileFeeBo;
            } else {
                smileFeeBo.setSize(smileFeeBo.getSize() + choesphotosNum);
                smileFeeBo.setAmount(new BigDecimal(0));
            }
        } else if (choesphotosNum >= feeShootMax) {
            int outNum = choesphotosNum - feeShootMax;
            feeAmountFull = feeAmountFull.multiply(BigDecimal.valueOf(outNum));
            smileFeeBo.setAmount(feeAmount.add(feeAmountFull));
            smileFeeBo.setSize(choesphotosNum);
            return smileFeeBo;
        } else {
            smileFeeBo.setAmount(feeAmount);
            smileFeeBo.setSize(choesphotosNum);
        }

        return smileFeeBo;
    }
}
